package org.msfj.generator;

import cn.hutool.core.collection.CollectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.Map;

public class TableUtil {

    private static Logger logger = LoggerFactory.getLogger(TableUtil.class);

    private Connection connection = null;
    static String url;
    static String username;
    static String password;
    static String dirver;

    public void setDataBaseInfo(String dirver, String url, String username, String password) {
        this.dirver = dirver;
        this.url = url;
        this.username = username;
        this.password = password;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public Connection getConnection() throws SQLException {
        try {
            Class.forName(dirver);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DriverManager.getConnection(url, username, password);
    }

    private String formatTableName(String name) {
        String[] split = name.split("_");
        if (split.length > 1) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < split.length; i++) {
                String tempName = split[i].substring(0, 1).toUpperCase() + split[i].substring(1, split[i].length());
                sb.append(tempName);
            }

            return sb.toString();
        }
        String tempName = split[0].substring(0, 1).toUpperCase() + split[0].substring(1, split[0].length());
        return tempName;
    }

    public String getTablesNameToClassName(String tableName) {
        String tempTables = formatTableName(tableName);
        return tempTables;
    }

    public Map<String, Column> getColumns(String tableName) {
        PreparedStatement pst = null;
        Map<String, Column> columns = CollectionUtil.newHashMap();
        try {
            connection = getConnection();
            pst = connection.prepareStatement("select column_name, data_type, column_comment " +
                    "from information_schema.columns Where table_name = '" + tableName + "'");
            ResultSet resultSet = pst.executeQuery();
            while (resultSet.next()) {
                String columnName = formatTableName(resultSet.getString(1));
                String lowerName = columnName.substring(0, 1).toLowerCase() + columnName.substring(1, columnName.length());
                if (lowerName.indexOf("id") >= 0) {
                    continue;
                } else if (lowerName.indexOf("enable") >= 0) {
                    continue;
                } else if (lowerName.indexOf("createBy") >= 0) {
                    continue;
                } else if (lowerName.indexOf("createTime") >= 0) {
                    continue;
                } else if (lowerName.indexOf("updateBy") >= 0) {
                    continue;
                } else if (lowerName.indexOf("updateTime") >= 0) {
                    continue;
                } else if (lowerName.indexOf("search") >= 0) {
                    continue;
                } else {
                    String javaType = resultSet.getString(2);
                    javaType = javaType.replace("java.lang.", "");
                    String columnLabel = resultSet.getString(3);
                    Column column = new Column();
                    column.setName(lowerName);
                    column.setJavaType(javaType);
                    column.setAlias(columnLabel);
                    columns.put(lowerName, column);
                }
            }
        } catch (
                SQLException e)

        {
            logger.error(e.getMessage());
        } finally

        {
            try {
                pst.close();
                pst = null;
            } catch (SQLException e) {
                logger.error(e.getMessage());
            }
        }
        return columns;
    }
}